package controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;

import dao.ZhCN_to16;
import entity.JsonMsg;
import entity.User;
import service.UserService;
import service.impl.UserServiceImpl;

/**
 * Servlet implementation class ChangePhoneServlet
 */
@WebServlet("/ChangePhoneServlet")
public class ChangePhoneServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangePhoneServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		UserService userService = new UserServiceImpl();
		
		String input_new_phone=request.getParameter("input_new_phone");
		String input_new_phone1=request.getParameter("input_new_phone1");
		String input_change_phone=request.getParameter("input_change_phone");
		Integer uid = Integer.parseInt(request.getParameter("uid"));
		User user = userService.findUserById(uid);
		JsonMsg jsonMsg=null;
		
		if(!user.getPhone().equals(input_change_phone)){
			jsonMsg = JsonMsg.makeFail("原号码错误！请重试！", null);
			jsonMsg.setData(1);
		}else if(input_new_phone==""){
			jsonMsg = JsonMsg.makeFail("新号码不能为空！", null);
			jsonMsg.setData(2);
		}
		else if(!input_new_phone.equals(input_new_phone1)){
			jsonMsg = JsonMsg.makeFail("两次输入不同！请重试！", null);
			jsonMsg.setData(3);
		}else if(input_new_phone.equals(input_change_phone)){
			jsonMsg = JsonMsg.makeFail("新号码与旧号码相同！请重试！", null);
			jsonMsg.setData(4);
		}else{
			boolean isok = userService.updatePhone(uid, input_new_phone);

			System.out.println(user.getPhone());
			if(isok){
				jsonMsg=JsonMsg.makeSuccess("修改号码成功",null);
			}else{
				jsonMsg=JsonMsg.makeFail("修改失败", null);
				
			}
		}
		// 序列化jsonMsg
		ObjectMapper objectMapper = new ObjectMapper();
		String writeValueAsString = objectMapper.writeValueAsString(jsonMsg);
		out.println(ZhCN_to16.Aescape(writeValueAsString));
	}

}
